from fastapi.exceptions import (
    HTTPException,
    RequestValidationError,
    ResponseValidationError,
)
from fastapi.requests import Request
from fastapi.responses import JSONResponse
from tortoise.exceptions import DoesNotExist,IntegrityError

class SettingNotFound(DoesNotExist):
    pass

async def DoesNotExistHandler(req :Request,exc:DoesNotExist) -> JSONResponse:
    content = dict(
        code = 404,
        msg = f"object has not found,exc ;{exc},query_params:{req.query_params}",
    )
    return JSONResponse(status_code=404,content=content)

async def IntegrityHandler(_:Request,exc:IntegrityError) -> JSONResponse:
    content = dict(
        code = 500,
        msg = f"IntegrityError,exc:{exc}",
    )
    return JSONResponse(status_code=500,content=content)

async def HttpExcHandle(_:Request,exc:HTTPException) -> JSONResponse:
    content = dict(code=exc.status_code,msg=exc.detail,data=None)
    return JSONResponse(status_code=exc.status_code,content=content)


async def ReuestValidationHandle(_:Request,exc:RequestValidationError) -> JSONResponse:
    content = dict(code=422,msg=f"RequestValidationError,exc:{exc}")
    return JSONResponse(status_code=422,content=content)

async def ResponseValidationHandle(_:Request,exc:ResponseValidationError) -> JSONResponse:
    content = dict(code=500,msg=f"ResponseValidationError,exc:{exc}")
    return JSONResponse(status_code=500,content=content)
